//
//  ZZSelector.m
//  RecorderTest
//
//  Created by 张彦东 on 2017/5/24.
//  Copyright © 2017年 Z. All rights reserved.
//

#import "ZZSelector.h"
#import "ZZSelectorCell.h"

@interface ZZSelector () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, weak) UIButton *closeButton;
@property (nonatomic, weak) UIButton *saveButton;
@property (nonatomic, weak) UITableView *tableView;
@property (nonatomic, weak) UIView *line;
@property (nonatomic, weak) UIView *whiteView;
@end

@implementation ZZSelector
{
    // 已选择的行
    NSSet *_selectedRows;
}

#pragma mark - public
- (void)show {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:self];
    
    [self.tableView reloadData];
    [self showAnimation];
}
- (void)showAnimation {
    _whiteView.transform = CGAffineTransformMakeTranslation(0, CGRectGetHeight(_whiteView.frame));
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 1;
        _whiteView.transform = CGAffineTransformIdentity;
    }];
}

#pragma mark - life cycle
- (void)willMoveToSuperview:(UIView *)newSuperview {
    [super willMoveToSuperview:newSuperview];
    self.frame = newSuperview.bounds;
    [self setupFrame];
}
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
        [self setupViews];
        self.alpha = 0;
    }
    return self;
}
- (void)setupViews {
    UIView *whiteView = [[UIView alloc] init];
    [self addSubview:whiteView];
    _whiteView = whiteView;
    whiteView.backgroundColor = [UIColor whiteColor];
    
    UITableView *tableView = [[UITableView alloc] init];
    [whiteView addSubview:tableView];
    _tableView = tableView;
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.contentMode = UIViewContentModeCenter;
    tableView.rowHeight = 50;
    tableView.showsVerticalScrollIndicator = NO;
    tableView.backgroundColor = [UIColor whiteColor];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    UIButton *save = [[UIButton alloc] init];
    [whiteView addSubview:save];
    _saveButton = save;
    save.titleLabel.font = [UIFont systemFontOfSize:14];
    [save setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [save setBackgroundColor:[UIColor redColor]];
    [save setTitle:@"保存" forState:UIControlStateNormal];
    [save addTarget:self action:@selector(save) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *close = [[UIButton alloc] init];
    [whiteView addSubview:close];
    _closeButton = close;
    close.titleLabel.font = [UIFont systemFontOfSize:14];
    [close setTitleColor:[[UIColor blackColor] colorWithAlphaComponent:0.8] forState:UIControlStateNormal];
    [close setBackgroundColor:[[UIColor lightGrayColor] colorWithAlphaComponent:0.9]];
    [close setTitle:@"关闭" forState:UIControlStateNormal];
    [close addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside];
    
    UIView *line = [[UIView alloc] init];
    [whiteView addSubview:line];
    _line = line;
    line.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.8];
}

- (void)setupFrame {
    CGFloat viewWidht = self.frame.size.width;
    CGFloat viewHeight = self.frame.size.height;
    CGFloat tableViewHeight = MIN(_options.count * 50, 200);
    CGFloat buttonHeight = 50;
    CGFloat whiteWidth = viewWidht;
    CGFloat whiteHeight = tableViewHeight + 1 + buttonHeight;
    _whiteView.frame = CGRectMake(0, viewHeight - whiteHeight, whiteWidth, whiteHeight);
    _tableView.frame = CGRectMake(0, 0, whiteWidth, tableViewHeight);
    _line.frame = CGRectMake(0, CGRectGetMaxY(_tableView.frame), whiteWidth, 1);
    if (self.isMultipie) {
        _closeButton.frame = CGRectMake(0, CGRectGetMaxY(_line.frame), whiteWidth / 2, buttonHeight);
        _saveButton.frame = CGRectMake(CGRectGetMaxX(_closeButton.frame), CGRectGetMaxY(_line.frame), whiteWidth / 2, buttonHeight);
    } else {
        _closeButton.frame = CGRectMake(0, CGRectGetMaxY(_line.frame), whiteWidth, buttonHeight);
    }
}

#pragma mark - setter
- (void)setMultiple:(BOOL)multiple {
    _multiple = multiple;
    _tableView.allowsMultipleSelection = multiple;
}

#pragma mark - button click
- (void)save {
    if ([_delegate respondsToSelector:@selector(selector:didSelectedRowAtIndex:identifier:)]) {
        NSArray *selectedArray = _tableView.indexPathsForSelectedRows;
        NSMutableSet *optSet = [[NSMutableSet alloc] init];
        for (NSIndexPath *path in selectedArray) {
            [optSet addObject:@(path.row)];
        }
        [_delegate selector:self didSelectedRows:optSet identifier:_identifier];
    }
    [self close];
}
- (void)close {
    [UIView animateWithDuration:0.5 animations:^{
        _whiteView.transform = CGAffineTransformMakeTranslation(0, CGRectGetHeight(_whiteView.frame));
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

#pragma mark - UITableViewDataSource, UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _options.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZZSelectorCell *cell = [ZZSelectorCell cellWithTableView:tableView];
    cell.selectLabelText = _options[indexPath.row];
    if (self.isMultipie && _selectedRows.count && [_selectedRows containsObject:@(indexPath.row)]) {
        [tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.isMultipie) return;
    
    if ([_delegate respondsToSelector:@selector(selector:didSelectedRowAtIndex:identifier:)]) {
        [_delegate selector:self didSelectedRowAtIndex:indexPath.row identifier:_identifier];
    }
    [self close];
}

@end
